<template>
  <div class="GSupervisionArea">
    <filterPanel filterName="GSupervisionAreaFilter" filterTitle="监管辖区查询" :filterList="filterData"
                 @filterChange="filterChange"></filterPanel>
    <tablePanel tableName="GSupervisionAreaTable" :tableLoading="loading" tableTitle="监管辖区列表" :tableList="tableList"
                :tableData="tableData" :total="total" :currentPage="form.page_start" :pageSize="form.page_size"
                @sizeChange="sizeChange" @pageChange="pageChange" @tableClick="tableClick">
      <zy-button type="primary" @click="add">新增</zy-button>
      <zy-button @click="exportExcel">导出列表</zy-button>
    </tablePanel>
    <zyModel :show="show" :title="title" @submit="submit" @close="close('modify')">
      <zy-modal-info :model-info="editInfo" cellNum="2"></zy-modal-info>
    </zyModel>
    <policeModel :show="showPolice" :regulated_id="regulated_id" @close="close"></policeModel>
    <enterpriseModel :show="showEnterprise" :regulated_id="regulated_id" @close="close"></enterpriseModel>
    <cooperativeModel :show="showCoop" :regulated_id="regulated_id" @close="close"></cooperativeModel>
  </div>
</template>

<script>
	import {mapGetters} from "vuex";
	import policeModel from "./policeModel";
	import enterpriseModel from "./enterpriseModel";
	import cooperativeModel from "./cooperativeModel";

	export default {
		name: "GSupervisionArea",
		props: {},
		data() {
			return {
				showEnterprise: false,
				showCoop: false,
				showPolice: false,
				show: false,
				title: '',
				editInfo: [
					{
						title: '辖区名称',
						name: 'regulated_name',
						type: 'input',
						value: '',
						titleClass: 'required'
					},
					{
						title: '联系电话',
						name: 'regulated_mobile',
						type: 'input',
						value: '',
						titleClass: 'required'
					},
					{
						title: '辖区地址',
						name: 'regulated_address',
						type: 'input',
						value: '',
						titleClass: 'required'
					},
				],
				currentPath: '/supervision_area_list',
				filterData: [
					{
						title: '辖区名称',
						name: 'regulated_name',
						type: 'input',
						value: '',
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
					{
						title: '辖区地址',
						name: 'regulated_address',
						type: 'input',
						value: '',
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
					{
						title: '联系电话',
						name: 'regulated_mobile',
						type: 'input',
						value: '',
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						name: 'registration_date',
						type: 'date',
						value: [],
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
				],
				form: {
					regulated_name: '',
					regulated_address: '',
					regulated_mobile: '',
					registration_date: [],
					page_start: 1,
					page_size: 10
				},
				loading: false,
				tableList: [
					{
						title: '辖区名称',
						width: 120,
						name: 'regulated_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '联系电话',
						width: 120,
						name: 'regulated_mobile',
						isShow: true,
						isSelected: true
					},
					{
						title: '辖区地址',
						width: 120,
						name: 'regulated_address',
						isShow: true,
						isSelected: true
					},
					{
						title: '民警数量',
						width: 100,
						name: 'regulate_count_desc',
						btns: [{
							type: 'look_regulate_count',
							name: '',
							code: 'regulate_count'
						}],
						isShow: true,
						isSelected: true
					},
					{
						title: '物流企业数量',
						width: 100,
						name: 'company_count_desc',
						btns: [{
							type: 'look_company_count',
							name: '',
							code: 'company_count'
						}],
						isShow: true,
						isSelected: true
					},
					{
						title: '合作站点数量',
						width: 100,
						name: 'cooper_count_desc',
						btns: [{
							type: 'look_cooper_count',
							name: '',
							code: 'cooper_count'
						}],
						isShow: true,
						isSelected: true
					},
					{
						title: '备注',
						width: 120,
						name: 'remark',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						width: 200,
						name: 'create_time',
						isShow: true,
						isSelected: true
					},
					{
						title: '操作',
						width: 200,
						name: 'handler',
						fixed: 'right',
						btns: [
							{
								type: 'modify',
								name: '修改'
							},
							{
								type: 'delete',
								name: '删除',
								btnType: 'danger'
							},
						],
						isSelected: true
					}
				],
				tableData: [],
				total: 0,
				download_url: '',
				regulated_id: ''
			};
		},
		computed: {
			postData() {
				let data = {
					regulated_name: this.form.regulated_name,
					regulated_address: this.form.regulated_address,
					regulated_mobile: this.form.regulated_mobile,
					pagination: {
						page_start: this.form.page_start,
						page_size: this.form.page_size
					}
				};
				if (this.form.registration_date && this.form.registration_date.length > 0) {
					data.start_created_time = this.form.registration_date[0] / 1000;
					data.end_created_time = this.form.registration_date[1] / 1000;
				}
				return data;
			},
			...mapGetters(['authority']),
			isAdd() {
				// return this.authority[this.currentPath] && this.authority[this.currentPath].i;
			},
			isModify() {
				// return this.authority[this.currentPath] && this.authority[this.currentPath].m;
			},
			isDel() {
				// return this.authority[this.currentPath] && this.authority[this.currentPath].d;
			},
		},
		watch: {},
		components: {policeModel, enterpriseModel, cooperativeModel},
		methods: {
			//导出
			exportExcel() {
				this.$import_excal(this.download_url || "");
			},
			// 新增
			add() {
				this.show = true;
				this.title = '新增监管辖区'
			},
			close(type) {
				switch (type) {
					case 'coop':
						this.showCoop = false;
						break;
					case 'police':
						this.showPolice = false;
						break;
					case 'enterprise':
						this.showEnterprise = false;
						break;
					case 'modify':
						this.show = false;
						this.editInfo = this.editInfo.filter(item => {
							item.value = '';
							return item
						});
						break;
				}
			},
			submit() {
				let postData = {};
				if (this.title == '编辑监管辖区') {
					postData['regulated_id'] = this.regulated_id;
				}
				this.editInfo.forEach(item => {
					if (!item.readonly) {
						postData[item.name] = item.value;
					}
				});
				this.$axios.post(this.$api.g_regulatedAddEditInfo, {
					...postData
				}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.close('modify');
						this.getData();
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			sizeChange(val) {
				this.form.page_size = val;
				this.getData()
			},
			pageChange(val) {
				this.form.page_start = val;
				this.getData()
			},
			filterChange(data) {
				this.form = {...this.form, ...data};
				this.getData();
			},
			getData() {
				this.loading = true;
				this.tableData = [];
				this.$axios.post(this.$api.g_regulatedList, {...this.postData}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.tableData = data.list.filter(item => {
							item.regulate_count_desc = '';
							item.look_regulate_count = true;
							item.company_count_desc = '';
							item.look_company_count = true;
							item.cooper_count_desc = '';
							item.look_cooper_count = true;
							item.modify = this.isModify || true;
							item.delete = this.isDel || true;
							return item;
						}) || [];
						this.download_url = data.download_url || ''
						this.total = data.pagination.total_count || 0;
					} else {
						this.total = 0
						this.tableData = [];
					}
				}).catch(ret => {
					console.log(ret)
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			tableClick(data, type) {
				switch (type) {
					case 'look_regulate_count':
						this.regulated_id = String(data.regulated_id);
						this.showPolice = true;
						break;
					case 'look_company_count':
						this.regulated_id = String(data.regulated_id);
						this.showEnterprise = true;
						break;
					case 'look_cooper_count':
						this.regulated_id = String(data.regulated_id);
						this.showCoop = true;
						break;
					case 'modify':
						this.show = true;
						this.title = '编辑监管辖区';
						this.regulated_id = String(data.regulated_id);
						this.setEditInfo(data)
						break;
					case 'delete':
						this.deleted(data.regulated_id);
						break;
				}
			},
			// 删除
			deleted(regulated_id) {
				this.$confirm("此操作将永久删除该辖区, 是否继续?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
					customClass: 'message',
				}).then(() => {
					this.$axios.post(this.$api.g_regulatedDetele, {regulated_id}).then(res => {
						let {code, msg} = res;
						if (code === 200) {
							this.$message.success("删除成功!");
							this.getData();
						} else {
							this.$message.error(msg);
						}
					})
				}).catch(() => {
					this.$message.info("已取消删除");
				});
			},
			setEditInfo(data) {
				this.editInfo = this.editInfo.filter(item => {
					for (let key in data) {
						if (item.name === key) {
							item.value = data[item.name];
						}
					}
					return item
				});
				console.log(this.editInfo)
			},
		},
		mounted() {
			this.getData()
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/government/personnelManage/supervisionAreaList/index.less">

</style>
